<?

/*require_once 'Error.class.php';
require_once 'SpotLight/core/HTTP/Session.class.php';
*/
/**
 * Errors collector. Seve himself into session. 
 * Provide mechanism to save errors that was appears in actions.
 * @package SpotLight.core.error
 * @author asmhack
 * @version since begin
 * @license GPL
 * @copyright Serhiy Zaporozhets'
 */
class ErrorsCollector {
   
   private $errors;
   
   private function __construct() {
      $this->errors = array ();
   }
   
   private function __clone() {
   }
   
   public static function getInstance() {
      if (! Session::isExist ( 'EC', false ))
         Session::putSystemValue ( 'EC', new ErrorsCollector ( ) );
      return Session::getSystemValue ( 'EC' );
   }
   
   public function selfUpdate() {
      Session::putSystemValue ( 'EC', $this );
   }
   
   public function setError($error, $key = null) {
      if (in_array ( $error, array_values ( $this->errors ) ))
         return;
      if ($key == null)
         $this->errors [] = $error;
      else
         $this->errors [$key] = $error;
      $this->selfUpdate ();
   }
   
   public function getError($key) {
      if (array_key_exists ( $key, $this->errors ))
         return $this->errors [$key];
      else
         return '';
   }
   
   public function getAllErrors() {
      return $this->errors;
   }
   
   public function clear() {
      unset ( $this->errors );
      $this->__construct ();
      $this->selfUpdate ();
   }
   
   public function isAnyErrors() {
      return sizeof ( $this->errors ) != 0;
   }

}
?>